package indi.fritz.bigmap.volme.model;

import indi.fritz.bigmap.util.ByteUtil;

public final class DataOffset implements StorageData{
	private static final long serialVersionUID = -7912343499569625597L;
	
	public static int SIZE = 13;
	public static int EXIST_INDEX = 0;
	
	private long start;
	private int length;
	private boolean exist;
	
	public DataOffset(){
		this.exist = false;
	}
	
	public DataOffset(long start,int length, boolean exist){
		this.start = start;
		this.length = length;
		this.exist = exist;
	}
	
	public long getStart() {
		return start;
	}
	public void setStart(long start) {
		this.start = start;
	}
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public boolean isExist() {
		return exist;
	}
	public void setExist(boolean exist) {
		this.exist = exist;
	}

	@Override
	public byte[] getByte() {
		byte[] existByte = ByteUtil.booleanToByte(exist);
		byte[] startByte = ByteUtil.longToByte(start);
		byte[] lengthByte = ByteUtil.intToByteArray(length);
		return ByteUtil.merges(new byte[][] { existByte, startByte, lengthByte });
	}
	
	public static DataOffset format(byte[] bs){
		boolean exist = ByteUtil.byteToBoolean(bs[0]);
		long start = ByteUtil.byteToLong(ByteUtil.sub(bs, 1, 8));
		int length = ByteUtil.byteArrayToInt(ByteUtil.sub(bs, 9, 4));
		return new DataOffset(start, length, exist);
	}
	

	@Override
	public String toString() {
		return "DataOffset [start=" + start + ", length=" + length + ", exist=" + exist + "]";
	}
}
